package com.jch.Dao;

import com.entity.User;

import java.sql.SQLException;
import java.util.List;


public class UserDao  extends BaseDao {
        public List<User> queryUsersById(Object...params) {
        String sql = "select * from user where uName = ? and uPass = ? ";
        try {
            return queryForListObject(sql, User.class, params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<User> queryUsersByuName(Object...params) {
        String sql = "select * from user where uName = ? ";
        try {
            return queryForListObject(sql, User.class, params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    public List<User> queryUsersByuPhone(Object...params) {
        String sql = "select * from user where uphone = ? ";
        try {
            return queryForListObject(sql, User.class, params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public  int InsertValues(Object...params){
        String sql="insert into user(uName, uPass, uType,uphone) values(?,?,?,?)";

        try {
            int i = queryRunner.update(sql, params);
            return i;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public  List<User>  queryUserByBalance(Object...params){
        String sql="select balance from user where uName=?";
        try {
            return  queryForListObject(sql, User.class, params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  null;
    }





    public  int updateAddBalance(Object...params){
        String sql="update  user set balance = balance+? where uName=?";

        try {
            int i = queryRunner.update(sql, params);
            return i;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public  int updateReduceBalance(Object...params){
        String sql="update  user set balance = balance-? where uName=?";

        try {
            int i = queryRunner.update(sql, params);
            return i;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
//
//    public List<User> queryUser(){
//        String sql="select * from user";
//        try {
//            return findList(User.class,sql);
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
//
    public User queryUser(Object...params) {
        String sql = "select * from user where uName=?";

            return findBean(User.class, sql, params);


    }
    public List<User> queryUsers() {
        String sql = "select * from user";
        return findList(User.class, sql);
    }

    /**
     * 根据编号查询单条数据
     *
     * @param id
     * @return
     */
    public User queryUser(int id) {
        String sql = "select * from user where uId=?";
        Object[] params = {id};
        return findBean(User.class, sql, params);
    }

    /**
     * 根据实体类数据更新用户
     *
     * @param user
     * @return
     */
    public int updateUser(User user) {
        String sql = "update user set uPass=?  where uphone=?";
        Object[] params = {user.getuPass(),user.getUphone()};
        return update(sql, params);
    }

    public int updateUseralterPSW(Object...params) {
        String sql = "update user set uPass=?  where uName=?";
        return update(sql, params);
    }


    public int updateUserByBalance(User user) {
        String sql = "update user set balance=? +balance";
        Object[] params = {user.getBalance()};
        return update(sql, params);
    }



    /**
     * 根据实体类数据更新用户
     *
     * @param user
     * @return
     */
    public int updateUser2(User user) {
        String sql = "update user set uName =?,uPass=?,balance=?,uType=?,uphone=?  where uId=?";
        Object[] params = {user.getuName(),user.getuPass(),user.getBalance()
                ,user.getuType(),user.getUphone(),user.getUid()};
        return update(sql, params);
    }

    /**
     * 根据id删除一条数据
     *
     * @param id
     * @return
     */
    public int deleteUser(int id) {
        String sql = "delete from user where uId=?";
        Object[] params = {id};
        return update(sql, params);
    }

    /**
     * 根据用户对象增加一条数据
     *
//     * @param user
     * @return
     */
    public int insertUser(User user) {
        String sql = "insert into user values(null,?,?,?,?,?)";
        Object[] params = {user.getuName(),user.getuPass(),user.getBalance(),user.getuType(),user.getUphone()};
        return update(sql, params);
    }


    public User queryUserBalanceByuName(Object...params){
        String sql="select * from user where uName=?";

        return findBean(User.class,sql);
    }



    public static void main(String[] args) {
        UserDao userDao = new UserDao();
//        List<User> users = userDao.queryUsersById("admin", "admin");
//        for (User user : users) {
//            System.out.println(user);
//        }
//         int i = userDao.InsertValues("xg", "123", 0);
//        System.out.println("插入了"+i+"条数据");

//        int i = userDao.updateAddBalance(150,"zxx");
//        System.out.println("更新了"+i+"条数据");

//        int i = userDao.updateReduceBalance(150,"zxx");
//        System.out.println("更新了"+i+"条数据");

//        List<User> users = userDao.queryUserByBalance("zxx");
//        for (User user : users) {
//            System.out.println(user);
//        }


    }


}




